﻿using Microsoft.Playwright;
using System.Threading.Tasks;

namespace OnlineStore.Common
{
    public abstract class NavigationUtils
    {
        /// <summary>
        /// 通过指定方式，匹配locator对象指定元素,默认为 Locator
        /// </summary>
        /// <param name="locator">locator 对象</param>
        /// <param name="request">匹配元素的方式</param>
        /// <param name="elementPath">元素匹配表达式</param>
        /// <returns>返回匹配到的元素 ILocator 对象</returns>
        public ILocator Request(ILocator locator, string request, string elementPath)
        {
            try
            {
                switch (request)
                {
                    case "Locator":
                        return locator.Locator(elementPath);
                    case "GetByText":
                        return locator.GetByText(elementPath);
                    case "GetByLabel":
                        return locator.GetByLabel(elementPath);
                    case "GetByTestId":
                        return locator.GetByTestId(elementPath);
                    case "GetByTitle":
                        return locator.GetByTitle(elementPath);
                    case "GetByAltText":
                        return locator.GetByAltText(elementPath);
                    case "GetByPlaceholder":
                        return locator.GetByPlaceholder(elementPath);
                    default:
                        return locator.Locator(elementPath);
                }
            }
            catch
            {
                throw;
            }
            
        }

        /// <summary>
        /// 通过指定方式，匹配 IPage 对象指定元素,默认为 Locator
        /// </summary>
        /// <param name="page">IPage 对象</param>
        /// <param name="request">匹配元素的方式</param>
        /// <param name="elementPath">元素匹配表达式</param>
        /// <returns>返回匹配到的元素 ILocator 对象</returns>
        public ILocator Request(IPage page, string request, string elementPath)
        {
            try
            {
                switch (request)
                {
                    case "Locator":
                        return page.Locator(elementPath);
                    case "GetByText":
                        return page.GetByText(elementPath);
                    case "GetByLabel":
                        return page.GetByLabel(elementPath);
                    case "GetByTestId":
                        return page.GetByTestId(elementPath);
                    case "GetByTitle":
                        return page.GetByTitle(elementPath);
                    case "GetByAltText":
                        return page.GetByAltText(elementPath);
                    case "GetByPlaceholder":
                        return page.GetByPlaceholder(elementPath);
                    default:
                        return page.Locator(elementPath);
                }
            }
            catch
            {
                throw;
            }
            
        }

        /// <summary>
        /// 通过指定方式，匹配 IFrameLocator 对象指定元素,默认为 Locator
        /// </summary>
        /// <param name="frame">IFrameLocator 对象</param>
        /// <param name="request">匹配元素的方式</param>
        /// <param name="elementPath">元素匹配表达式</param>
        /// <returns>返回匹配到的元素 ILocator 对象</returns>
        public ILocator Request(IFrameLocator frame, string request, string elementPath)
        {
            try
            {
                switch (request)
                {
                    case "Locator":
                        return frame.Locator(elementPath);
                    case "GetByText":
                        return frame.GetByText(elementPath);
                    case "GetByLabel":
                        return frame.GetByLabel(elementPath);
                    case "GetByTestId":
                        return frame.GetByTestId(elementPath);
                    case "GetByTitle":
                        return frame.GetByTitle(elementPath);
                    case "GetByAltText":
                        return frame.GetByAltText(elementPath);
                    case "GetByPlaceholder":
                        return frame.GetByPlaceholder(elementPath);
                    default:
                        return frame.Locator(elementPath);
                }
            }
            catch
            {
                throw;
            }

        }


        /// <summary>
        /// ILocator对象执行指定操作,默认为 ClickAsync
        /// </summary>
        /// <param name="locator">ILocator 对象</param>
        /// <param name="command">需要执行的操作行为</param>
        public async Task Execute(ILocator locator,string command)
        {
            try
            {
                switch (command)
                {
                    case "CheckAsync":
                        await locator.CheckAsync();
                        break;
                    case "ClearAsync":
                        await locator.ClearAsync();
                        break;
                    case "DblClickAsync":
                        await locator.DblClickAsync();
                        break;
                    case "ClickAsync":
                        await locator.ClickAsync();
                        break;
                    default:
                        await locator.ClickAsync();
                        break;
                }
            }
            catch
            {
                throw;
            }
            
        }

        /// <summary>
        /// IElementHandle 对象执行指定操作,默认为 ClickAsync
        /// </summary>
        /// <param name="elementHandle">IElementHandle 对象</param>
        /// <param name="command">需要执行的操作行为</param>
        public async Task Execute(IElementHandle elementHandle, string command)
        {
            try
            {
                switch (command)
                {
                    case "CheckAsync":
                        await elementHandle.CheckAsync();
                        break;
                    case "FillAsync":
                        await elementHandle.FillAsync(string.Empty);
                        break;
                    case "DblClickAsync":
                        await elementHandle.DblClickAsync();
                        break;
                    case "ClickAsync":
                        await elementHandle.ClickAsync();
                        break;
                    default:
                        await elementHandle.ClickAsync();
                        break;
                }
            }
            catch
            {
                throw;
            }

        }

        public abstract Task<bool> NavigationGo(string request, string execute, string elementPath);

    }
}
